package com.aaa.salesservice.service.impl;

import com.aaa.common.entity.Order;
import com.aaa.salesservice.dao.TbNewsDao;
import com.aaa.salesservice.entity.TbNews;
import com.aaa.salesservice.service.TbNewsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (TbNews)表服务实现类
 *
 * @author YuXin
 * @since 2020-11-05 14:53:49
 */
@Service("tbNewsService")
public class TbNewsServiceImpl implements TbNewsService {
    @Resource
    private TbNewsDao tbNewsDao;
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 远程调用order
     * 对HTTP和TCP客户端的行为进行大量空值
     * orderService 必须和调用的服务的向注册中心应用名称一致
     * /order/selectAll  必须和调用服务的方法请求url一致
     */

    @HystrixCommand(
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
            }
            ,//备用方法
            fallbackMethod = "selectAllOrderFallBack")
    private List<Order> selectAllOrder() {
        return restTemplate.getForObject("http://orderService/order/selectAll", List.class);
    }

    /**
     * 当远程接口出错时  本地备用方法
     */
    private List<Order> selectAllOrderFallBack() {
        Order order = new Order();
        order.setId(4444);
        order.setMemberUsername("ribbon-hystrix服务器报错，熔断测试备用方法");
        List<Order> list = new ArrayList<>();
        list.add(order);
        return list;
    }

    @Override
    public List<TbNews> selectAllNews() {
        return tbNewsDao.selectAllNews();
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Map queryById(Integer id) {
        TbNews tbNews = this.tbNewsDao.queryById(id);
        Map map = new HashMap();
        map.put("tbNews", tbNews);
        map.put("orders", this.selectAllOrder());
        return map;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<TbNews> queryAllByLimit(int offset, int limit) {
        return this.tbNewsDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param tbNews 实例对象
     * @return 实例对象
     */
    @Override
    public TbNews insert(TbNews tbNews) {
        this.tbNewsDao.insert(tbNews);
        return tbNews;
    }

    /**
     * 修改数据
     *
     * @param tbNews 实例对象
     * @return 实例对象
     */
    @Override
    public TbNews update(TbNews tbNews) {
        this.tbNewsDao.update(tbNews);
        return (TbNews) this.queryById(tbNews.getId()).get("tbNews");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tbNewsDao.deleteById(id) > 0;
    }
}